<template>
  <div>
    <el-form :model="formList" ref="formList" :rules="rules" label-position="top" size="medium">
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="项目名称">
            <el-input v-model="formList.project_name" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="包组名称">
            <el-input v-model="formList.package_name" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="标的名称" prop="target_name">
            <el-input v-model="formList.target_name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="标的服务期限" prop="serve_time">
            <el-input v-model="formList.serve_time">
              <span slot="append">个月</span>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="标的类型" prop="type">
            <el-cascader v-model="formList.type" value-key="type" placeholder="" :options="d_serveAll" class="wid">
            </el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0" v-if="otherShow">
          <el-form-item label="其他类型" prop="type_other">
            <el-input v-model="formList.type_other"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="排序">
            <el-input v-model="formList.sort"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="this.switch">
        <el-col :span="24" :offset="0">
          <el-form-item label="服务内容" prop="content">
            <el-input v-model="formList.content" type="textarea" :rows="6"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="center">
      <el-button type="danger" size="medium" @click="cancel">取消</el-button>
      <template v-if="this.switch">
        <el-button type="primary" size="medium" @click="onAdd" :loading="saveLoading" v-if="!formList.id">新增</el-button>
        <el-button type="primary" size="medium" @click="onEdit" :loading="saveLoading" v-else>修改</el-button>
      </template>
    </div>
  </div>
</template>
<script>
import { d_serveAll } from '@/utils/dicts.js'
export default {
  name: 'BidProject',
  components: {

  },
  props: {
    switch: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    formList: {
      type: Object,
      default: () => {
        return {}
      }
    },
  },
  data () {
    return {
      saveLoading: false,
      d_serveAll: d_serveAll,
      rules: {
        target_name: [
          { required: true, message: '请填写名称', trigger: 'blur' }
        ],
        serve_time: [
          { required: true, validator: this.$util.isFloat, trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'blur' }
        ],
        type_other: [
          { required: true, message: '请填写其他类型', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入服务内容', trigger: 'blur' },
        ],
      }
    }
  },
  created () {

  },
  mounted () {
  },
  methods: {
    async onAdd () {
      this.saveLoading = true
      if (!this.check()) {
        this.saveLoading = false
        return false
      }
      // const type = JSON.stringify(this.formList.type)
      this.$refs['formList'].validate(async (valid) => {
        if (valid) {
          const type = JSON.stringify(this.formList.type)
          const res = await this.$http('blockTargetAdd', {
            del_flag: '0',
            package_id: this.formList.package_id,
            subject_name: this.formList.target_name,
            serve_time: this.formList.serve_time,
            type: type,
            type_other: this.formList.type_other ? this.formList.type_other : '',
            sort: this.formList.sort,
            content: this.formList.content
          })
          this.$tips.success(res.errmsg)
          this.$emit('cancel')
          this.$emit('init')
          this.saveLoading = false
        } else {
          this.$tips.error('请检查必填项')
          this.saveLoading = false
          return false;
        }
      })
    },
    async onEdit () {
      this.saveLoading = true
      if (!this.check()) {
        this.saveLoading = false
        return false
      }
      this.$refs['formList'].validate(async (valid) => {
        if (valid) {
          const type = JSON.stringify(this.formList.type)
          const res = await this.$http('blockTargetUp', {
            del_flag: '0',
            id: this.formList.id,
            subject_name: this.formList.target_name,
            serve_time: this.formList.serve_time,
            type: type,
            type_other: this.formList.type_other ? this.formList.type_other : '',
            sort: this.formList.sort,
            content: this.formList.content
          })
          this.$tips.success(res.errmsg)
          this.$emit('cancel')
          this.$emit('init')
          this.saveLoading = false
        } else {
          this.$tips.error('请检查必填项')
          this.saveLoading = false
          return false;
        }
      })
    },
    check () {
      const regex = /^\+?[1-9]\d*$/
      if (this.formList.sort) {
        if (!regex.test(this.formList.sort)) {
          this.$tips.error('请填写整数')
          return false
        } else {
          return true
        }
      }
      return true
    },
    // changeType (item) {
    //   let other = '17,80,227,328,354,360'
    //   if (item) {
    //     let type = item[item.length - 1]
    //     if (other.indexOf(type) > -1) {
    //       this.otherShow = true
    //     } else {
    //       this.otherShow = false
    //     }
    //   }
    // },
    cancel () {
      this.$emit('cancel')
    }
  },
  computed: {
    otherShow () {
      let arr = this.formList.type
      let other = '17,80,227,328,354,360'
      if (arr) {
        let type = arr[arr.length - 1]
        if (other.indexOf(type) > -1) {
          return true
        } else {
          return false
        }
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
